package com.android.signhelper.ui;


import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.widget.SwipeRefreshLayout;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.method.LinkMovementMethod;
import android.text.style.URLSpan;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;


import com.android.signhelper.interfaces.MainContract;
import com.android.signhelper.R;
import com.android.signhelper.interfaces.OnClickListener;
import com.android.signhelper.presenter.MainPresenter;
import com.android.signhelper.base.BaseActivity;
import com.android.signhelper.bean.Student;
import com.android.signhelper.util.MyApplication;

import java.util.List;


public class MainActivity extends BaseActivity<MainContract.View,MainPresenter> implements MainContract.View,View.OnClickListener {

    private RecyclerView rvContent;
    private Toolbar toolbar;
    private SwipeRefreshLayout refresh;
    private RvStudentAdapter adapter;
    private FloatingActionButton fabSign;
    private ProgressBar progressBar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        getPresenter().initData();
    }

    private void initView() {
        progressBar = findViewById(R.id.progressBar);
        progressBar.setVisibility(View.INVISIBLE);
        fabSign = findViewById(R.id.fab_sign);
        toolbar = findViewById(R.id.toolbar);
        refresh = findViewById(R.id.srf_refresh);
        refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refreshMsg();
                refresh.setRefreshing(false);
            }
        });
        adapter=new RvStudentAdapter();
        adapter.setOnClickListener(new OnClickListener() {
            @Override
            public void setOnClickListener(int position) {
                Intent intent=new Intent(MainActivity.this, TestActivity.class);
                intent.putExtra("student",adapter.getList().get(position));
                startActivity(intent);
            }
        });
        rvContent = findViewById(R.id.rv_content);
        rvContent.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
        rvContent.setAdapter(adapter);
        setSupportActionBar(toolbar);
        fabSign.setOnClickListener(this);
        toolbar.setOverflowIcon(getResources().getDrawable(R.drawable.ic_more,null));
        fabSign.setImageResource(R.drawable.ic_sign);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main,menu);
        return true ;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.add_account:
                startActivity(new Intent(MainActivity.this,AddActivity.class));
                break;
            case R.id.about:
                SpannableStringBuilder stringBuilder = new SpannableStringBuilder("开源地址:https://gitee.com/ikata/SignHelper.git");
                stringBuilder.setSpan(new URLSpan("https://gitee.com/ikata/SignHelper.git"),5,stringBuilder.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
                AlertDialog.Builder builder=new AlertDialog.Builder(this);
                AlertDialog dialog=builder.setTitle("关于")
                        .setMessage(stringBuilder)
                        .setPositiveButton("好的", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        })
                        .create();
                dialog.show();
                ((TextView)dialog.findViewById(android.R.id.message)).setMovementMethod(LinkMovementMethod.getInstance());
                break;
        }
        return true;
    }

    private void refreshMsg() {
        for (Student student : adapter.getList()) {
            getPresenter().detail(student.getToken(), student.getUserName());
        }
    }

    @Override
    protected MainContract.View createView() {
        return this;
    }

    @Override
    protected MainPresenter createPresenter() {
        return new MainPresenter();
    }


    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        Student student=intent.getParcelableExtra("student");
        if (student!=null){
            //初始化数据获取token
            getPresenter().login(student.getUserName(),student.getPassword());
        }
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.fab_sign:
                progressBar.setVisibility(View.VISIBLE);
                getPresenter().sign(adapter.getList());
                break;
        }
    }

    @Override
    public void showData(List<Student> list) {
        adapter.addStudent(list);
        //刷新列表
        refreshMsg();
    }

    @Override
    public void showData(Student student) {
        adapter.update(student);
    }


    @Override
    public void onCompleted() {
        progressBar.setVisibility(View.INVISIBLE);
    }

    @Override
    public void showToast(final String msg) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(MainActivity.this,msg,Toast.LENGTH_LONG).show();
            }
        });
    }
}
